package com.wing.sys.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wing.common.Constants;
import com.wing.common.util.PageBean;
import com.wing.sys.domain.Code;
import com.wing.sys.domain.Role;
import com.wing.sys.service.CodeManager;
import com.wing.sys.service.RoleManager;

@Controller
@RequestMapping(value = "/sys/role")
public class RoleController {

	@Autowired
	private RoleManager roleManager;
	
	@Autowired
	private CodeManager codeManager;

	@RequestMapping(method = RequestMethod.GET)
	public String list(Model model) {
		return "sys/role/roleIndex";
	}
	
	@RequestMapping(value = "get/{url}", method = RequestMethod.GET)
	public String get(@PathVariable String url) {
		return "sys/role/"+url;
	}

	/**
	 * 保存一个角色
	 * @param user
	 * @return
	 */
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public @ResponseBody String add(Role role,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		role.setSite_id(siteId);
		roleManager.saveRole(role);
		return Constants.DEFAULT_AJAX_SUCCESS;
	}

	/**
	 * 打开角色编辑页面
	 * @param id
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "toEdit/{id}", method = RequestMethod.GET)
	public String toEdit(@PathVariable
	String id, Model model,String codeId) {
		Code c = null;
		Role role = new Role();
		if(!id.equals("0")){
			role = roleManager.findById(id);
			c = codeManager.findByCode(role.getRole_type_id());
		}else{
			c = codeManager.findById(codeId);
			role.setRole_type_id(c.getCode());
		}
		model.addAttribute("data", role);
		model.addAttribute("roleType", c.getName());
		return "sys/role/roleForm";
	}

	/**
	 * 删除多个角色
	 * @param request
	 * @param response
	 * @return
	 * @throws IOException
	 */
	@RequestMapping(value = "delroles", method = RequestMethod.POST)
	public String delroles(HttpServletRequest request,HttpServletResponse response) throws IOException {
		String chk [] = request.getParameterValues("chk");
		roleManager.deleteRoles(chk);
		response.getWriter().print(""+chk.length);
		return null;
	}
	
	/**
	 * ajax方式 获取一个角色列表 
	 * @return
	 */
	@RequestMapping(value = "list", method = RequestMethod.POST)
	public @ResponseBody PageBean list(int currentPage,
			String pageSize,String codeId,HttpServletRequest request) {
		String siteId = (String)request.getSession().getAttribute(Constants.SESSION_SITE_ID);
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  roleManager.findPageList(currentPage, _iPageSize,codeId,siteId);
	}
	
	/********************************角色用户维护   开始*******************************/
	/**
	 * ajax方式 获取该的角色所有用户
	 * @param id 角色ID
	 * @return
	 */
	@RequestMapping(value = "showusers", method = RequestMethod.POST)
	public @ResponseBody PageBean showusers(int currentPage,
			String pageSize,String roleId) {
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  roleManager.findPageUsersByRoleId(currentPage, _iPageSize,roleId);
	}
	
	/**
	 * ajax添加用户
	 * @return
	 */
	@RequestMapping(value = "addusers", method = RequestMethod.POST)
	public @ResponseBody String addusers(String users,String roleId) {
		String msg = Constants.DEFAULT_AJAX_SUCCESS;
		String userarray[] = users.split("\\|");
		roleManager.addUsersForRole(userarray,roleId);
		return msg;
	}
	/**
	 * ajax删除用户
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "delusers", method = RequestMethod.POST)
	public @ResponseBody String delusers(HttpServletRequest request){
		String msg = Constants.DEFAULT_AJAX_SUCCESS;
		String roleId = request.getParameter("roleId");
		String userarray[] = request.getParameterValues("chk");
		roleManager.deleteUsersForRole(userarray,roleId);
		return msg;
	}
	/********************************角色用户维护   结束*******************************/
	
	/********************************角色模块维护  开始*******************************/
	/**
	 * ajax方式 获取该的角色所有用户
	 * @param id 角色ID
	 * @return
	 */
	@RequestMapping(value = "showmods", method = RequestMethod.POST)
	public @ResponseBody PageBean showmods(int currentPage,
			String pageSize,String roleId){
		int _iPageSize = 0;
		if (pageSize == null) {
			_iPageSize = PageBean.DEFAULT_PAGESIZE;
		} else {
			_iPageSize= Integer.parseInt(pageSize);
		}
		return  roleManager.findPageModulesByRoleId(currentPage, _iPageSize,roleId);
	}

	
	/**
	 * ajax添加模块
	 * @return
	 */
	@RequestMapping(value = "addmods", method = RequestMethod.POST)
	public @ResponseBody String addmods(String modules,String roleId) {
		String msg = Constants.DEFAULT_AJAX_SUCCESS;
		String modarray[] = modules.split("\\|");
		roleManager.addModsForRole(modarray,roleId);
		return msg;
	}
	/**
	 * ajax删除模块
	 * @return
	 * @throws IOException 
	 */
	@RequestMapping(value = "delmods", method = RequestMethod.POST)
	public @ResponseBody String delmods(HttpServletRequest request){
		String msg = Constants.DEFAULT_AJAX_SUCCESS;
		String roleId = request.getParameter("roleId");
		String modarray[] = request.getParameterValues("chk");
		roleManager.deleteModsForRole(modarray,roleId);
		return msg;
	}
	/********************************角色模块维护   结束*******************************/
	
	
	
}
